<template>
  <el-card class="userinfo-container">
    <!-- 面板屑导航 -->
    <bread first="个人中心" second="基本资料"></bread>
    <el-form
      :model="infoForm"
      :rules="infoRuels"
      ref="infoRef"
      label-width="100px"
    >
      <el-form-item label="登录名称">
        <el-input v-model="infoForm.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="infoForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="infoForm.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="changeUser">提交修改</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updateUserInfoApi } from '@/api/user.js'
import { mapState } from 'vuex'
export default {
  name: '',
  data () {
    return {
      // 表单信息对象
      infoForm: {
        username: '',
        nickname: '',
        email: ''
      },
      // 验证规则对象
      infoRuels: {
        nickname: [
          {
            required: true,
            message: '昵称不能为空',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^\S{1,10}$/,
            message: '昵称是1-10个非空字符',
            trigger: ['blur', 'change']
          }
        ],
        email: [
          {
            required: true,
            message: '邮箱不能为空',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^([a-z0-9_.-]+)@([\da-z.-]+)\.([a-z.]{2,6})$/,
            message: '邮箱格式不正确',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  mounted () {
    // setTimeout(() => {
    //   this.infoForm = this.userInfo
    // }, 800)
    // this.infoForm = this.userInfo
    // // console.log(this.userInfo, 888)
  },
  watch: {
    userInfo (newVal, oldVal) {
      console.log(newVal, 111112222)
      this.infoForm = newVal
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    async changeUser () {
      /*
      1、点击之前
        1.1  获取数据展示到页面上
      2、点击之后
        2.1发送请求
        2.2 成功后给出成功的提示
        2.3 重置
       */
      const res = await updateUserInfoApi(this.infoForm)
      console.log(res, 333)
    },
    reset () {
      this.$refs.infoRef.resetFields()
    }
  }
}
</script>
<style lang='less'  scoped>
.el-form {
  width: 50%;
  margin: 30px 0;
}
</style>
